﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using rbac.share;

namespace rbac.services;

public class ServiceBase
{
    protected readonly IMapper _mapper;
    protected readonly IHttpContextAccessor _contextAccessor;
    protected int LoginUserId;

    protected ServiceBase()
    {
        // 从容器中获取服务实例
        _mapper = ServiceProviderLocator.Instance.GetService<IMapper>()!;
        _contextAccessor = ServiceProviderLocator.Instance.GetService<IHttpContextAccessor>()!;
        // 判断一下是否被授权了
        if (_contextAccessor.HttpContext!=null)
        {
            if (_contextAccessor.HttpContext.User.Identity!.IsAuthenticated)
            {
                LoginUserId = Convert.ToInt32(_contextAccessor.HttpContext.User.Claims
                    .FirstOrDefault(p => p.Type.Equals("UserId")).Value);
            }
        }
    }
}